<?php

namespace Admin\Controller;

class UserController extends BaseController
{
  /**
   * 会员列表显示
   */
  var $userObj;
  var $B;
  var $adminpower;
  public function __construct()
  {
    $adminid = $_SESSION['adminId'];
    $this->adminpower = M("Admin")->where(array("id"=>$adminid))->getField("adminpower");
    parent::__construct();
    $this->userObj = D('User');
    $this->B = M("Sys")->where(array("name"=>'currency_name'))->getField("value");
    $adminpower = explode(',',$this->adminpower);
    foreach($adminpower as $k => $v){
      $this->assign('power_'.$v, "isadmin");
    }
    $wallet_name = M("Sys")->where(array("name"=>"wallet_name"))->getField("value");
    $sysname = M("Sys")->where(array("name"=>"sys_name"))->getField("value");
    $seat_name = M("Sys")->where(array("name"=>"seat_name"))->getField("value");
    $recommend_name = M("Sys")->where(array("name"=>"recommend_name"))->getField("value");
    $this->assign('wallet_name', $wallet_name);
    $this->assign('sysname', $sysname);
    $this->assign('B', $this->B);

    $msg = M("Msg")->where(array('answer_date'=>0,"status"=>0))->select();
    foreach($msg as $k => $v){
      $username = M("User")->where(array("id"=>$v['user_id']))->getField("username");
      $msg[$k]['username'] = change_username($username);
    }
    $this->assign('msg', $msg);
    $this->assign('msgcount', count($msg));
  }

  /**
   * 首页
   */
  public function index()
  {
    $adminid = trim(session('adminId'));
    $adminname = M("Admin")->where("id = $adminid")->getField("name");
    $admintype = M("Admin")->where("id = $adminid")->getField("adminpower");
    $this->assign('admintype', $admintype);
    $this->assign('adminname', $adminname);
    $this->display();
  }
  public function auto()
  {
    //会员人数
    $list = M("User")->order("id DESC")->select();
    $this->assign('num', count($list));
    //昨日新增
    $beginYesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
    $endYesterday = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
    $y_list = M("User")->where(array("reg_time" => array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->select();
    $this->assign('y_num', count($y_list));
    //奖池金额
    $pond = M("Sys")->where(array("name"=>"pond"))->getField("value");
    $this->assign('pond', $pond);
    //业绩大比拼金额
    $listpond = M("Sys")->where(array("name"=>"list"))->getField("value");
    $this->assign('listpond', $listpond);

    //新用户列表
    $userlist = M("User")->where(array("id"=>array("gt",0)))->order("reg_time desc")->limit(8)->select();
    $this->assign('userlist', $userlist);

    //交易列表
    $salelist = M("Market")->where(array('is_del'=>0))->order("add_time desc")->limit(8)->select();
    foreach($salelist as $k=>$v){
      $salelist[$k]['username'] = M("User")->where(array("id"=>$v['user_id']))->getField("username");
    }
    $this->assign('salelist', $salelist);



    //业绩列表
    //当前业绩期数
    $now_yj_time = M("Sys")->where(array("name"=> 'now_yj_time'))->getField('value');
    $where['user_id'] = array("gt",0);
    $yejilist = M("Yeji")->where(array("yeji_id"=>$now_yj_time))->where($where)->select();

    $new_yejilist =array();
    foreach($yejilist as $k => $v){
      $act_time = M("User")->where(array("id"=>$v['user_id']))->getField("activate_time");
      if($act_time >0){   //必须捐赠过。
        $new_yejilist[$k] = $v;
      }
    }
    foreach($new_yejilist as $k=>$v){
      $new_yejilist[$k]['username'] = M("User")->where(array("id"=>$v['user_id']))->getField("username");
      $new_yejilist[$k]['time'] = M("User")->where(array("id"=>$v['user_id']))->getField("activate_time");
    }

    array_multisort(array_column($new_yejilist, 'num'), SORT_DESC, array_column($new_yejilist, 'time'), SORT_ASC, $new_yejilist); /*按推荐数排序*/
    $all_list = array_slice($new_yejilist, 0, 15);
    $this->assign('yejilist', $all_list);
    $this->display();
  }

  /**
   * 理财产品显示
   */
  public function kuangji()
  {
    $time = time();
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    if(empty(I('page'))){
      $_SESSION["keyword"] = '';
    }
    if($_SESSION["keyword"]){
      $keyword = $_SESSION["keyword"];
      $where['username']=array('like',"%$keyword%");
    }
    $this->assign("keyword", $_SESSION["keyword"]);
    $admin = M("Admin");
    $kuangji = M("Kuangji");
    $adminId = session('adminId');
    $alllist = $kuangji->where($where)->order("id DESC")->select();
    $list = $kuangji->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();

    foreach($list as $k => $v){
      $list[$k]['sy'] = $v['day_num']-$v['now_num'];
      $list[$k]['sy_m'] = $v['day_money']-$v['now_money'];
      //矿机是否到期
      $endtime = $v['addtime']+$v['cycle']*86400;
      if($endtime < $time){
        $list[$k]['end'] = 1;
      }else{
        $list[$k]['end'] = 0;
      }
    }

    $num = count($alllist);
    $this->assign('num', $num);
    $this->assign('kuangji', $list);
    //ypp 分页
    $page_all = ceil(($num)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", $num);
    $this->display();
  }
  /**
   * 用户投资记录显示
   */
  public function userkj()
  {
    $time = time();
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    if(empty(I('page'))){
      $_SESSION["keyword"] = '';
    }
    if($_SESSION["keyword"]){
      $keyword = $_SESSION["keyword"];
      $where['username']=array('like',"%$keyword%");
    }
    $this->assign("keyword", $_SESSION["keyword"]);
    $kuangji = M("Userkj");
    $alllist = $kuangji->where($where)->order("id DESC")->select();
    $list = $kuangji->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();

    foreach($list as $k => $v){
      //矿机是否到期
      if($v['endtime'] < $time || $v['status'] == 1){
        $list[$k]['end'] = 1;
      }else{
        $list[$k]['end'] = 0;
      }
      $kj = M("Kuangji")->where(array("id"=>$v["kj_id"]))->select();
      $list[$k]['name'] = $kj[0]['name'];
      $list[$k]['kjtime'] = $kj[0]['addtime'];
      $list[$k]['lilv'] = $kj[0]['lilv'];
      $list[$k]['day_num'] = $kj[0]['day_num'];
      $list[$k]['username'] =  M("User")->where(array("id"=>$v["user_id"]))->getField("username");
    }

    $num = count($alllist);
    $this->assign('num', $num);
    $this->assign('kuangji', $list);
    //ypp 分页
    $page_all = ceil(($num)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", $num);
    $this->display();
  }
  /**
   * 会员列表显示
   */
  public function clients()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    if(empty(I('page'))){
      $_SESSION["keyword"] = '';
    }
    if($_SESSION["keyword"]){
      $keyword = $_SESSION["keyword"];
      $where['username']=array('like',"%$keyword%");
    }
    $this->assign("keyword", $_SESSION["keyword"]);
    $admin = M("Admin");
    $user = M("User");
    $adminId = session('adminId');
    $alllist = $user->where($where)->order("id DESC")->select();
    $list = $user->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
    foreach($list as $k =>$v){
      $list[$k]['parentname'] = M("User")->where(array("id"=>$v['parent']))->getField("username");
      $list[$k]['usermoney'] = sctonum($list[$k]['usermoney']);
      $list[$k]['freeze_money'] = sctonum($list[$k]['freeze_money']);
      $childnum = M("User")->where(array("parent"=>$v['id']))->select();
      $list[$k]['child_num'] = count($childnum);
      $class_name = M("Userclass")->where(array("class"=>$v['user_class']))->getField("class_name");
      $list[$k]['class_name'] = $class_name?$class_name:"普通会员";

      //团队活跃度
      $users = M("User")->where(array("shiming"=>1))->select();
      //团队所有会员
      $childs = GetTeamMember($users,$v['id']);
      $childs = implode(',',$childs);
      if($childs){
        $jichu = M("User")->where(array("id"=>array("in",$childs)))->sum('huoyue');//基础活跃度
        $jichu = $jichu?$jichu:0;
        $jiacheng = M("User")->where(array("id"=>array("in",$childs)))->sum('huoyue_jc');//加成活跃度
        $tdhyd = $jichu + $jiacheng;//团队活跃度 = 基础活跃度+加成活跃度
      }
      $list[$k]['huoyue_tuan'] = $tdhyd?$tdhyd:0;

    }

    $num = count($alllist);
    $this->assign('num', $num);
    $this->assign('user', $list);
    //ypp 分页
    $page_all = ceil(($num)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", $num);
    $this->display();
  }
  //清除钱包地址
  public function clearqbdz()
  {
    $data["qbdz"] = '';
    $res = M("User")->where(1)->save($data);

      $this->success('用户钱包地址清除成功', U('User/clients'));

  }
  /**
   *  搜索
   */
  public function search()
  {
    $type = I("type");
    $id = I("userid");
    if(!$type){
      $this->redirect('User/index');
    }
    $keyword = I("keyword");
    $user = M("User");
    $username = M("User")->where(array("id"=>$id))->getField("username");
    $this->assign('username', $username);
    $this->assign('userid', $id);
    if($type == "clients"){
      //ypp 分页
      $page = !empty(I('page'))?I('page'):1;
      $pagenum = 10; //每页显示条数

      if($keyword){
        $_SESSION['keyword'] = $keyword;
        $where['username']=array('like',"%$keyword%");
        $this->assign('keyword',  $_SESSION['keyword']);
      }else{
        $_SESSION['keyword'] = "";
      }


      $all_loglist = $user->where($where)->order("id DESC")->select();
      $list = $user->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
      foreach($list as $k =>$v){
        $list[$k]['parentname'] = M("User")->where(array("id"=>$v['parent']))->getField("username");
        $list[$k]['usermoney'] = sctonum($list[$k]['usermoney']);
        $list[$k]['freeze_money'] = sctonum($list[$k]['freeze_money']);
        $childnum = M("User")->where(array("parent"=>$v['id']))->select();
        $list[$k]['child_num'] = count($childnum);
        $class_name = M("Userclass")->where(array("class"=>$v['user_class']))->getField("class_name");
        $list[$k]['class_name'] = $class_name?$class_name:"普通会员";
      }
      $num = count($list);
      //ypp 分页
      $page_all = ceil(count($all_loglist)/$pagenum);
      $this->assign("page_all", $page_all);
      $this->assign("page", $page);
      $this->assign("count", count($all_loglist));

      $this->assign('num', $num);
      $this->assign('user', $list);
      $this->assign('keyword', $keyword);
      $this->display('User/clients');
    }elseif($type == "moneylog"){
      //ypp 分页
      $page = !empty(I('page'))?I('page'):1;
      $pagenum = 10; //每页显示条数
      $id = I("post.userid");
      $all_loglist = M("Log")->where(array("user_id"=>$id,"msg"=>array("like","%$keyword%")))->order("id DESC")->select();
      $log_list = M("Log")->where(array("user_id"=>$id,"msg"=>array("like","%$keyword%")))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
      $num = count($log_list);
      $this->assign('num', $num);
      $this->assign('keyword', $keyword);
      $_SESSION['keyword'] = $keyword;
      foreach($log_list as $k => $v){
        $log_list[$k]['money'] = sctonum( $log_list[$k]['money']);
      }
      $this->assign('log_list', $log_list);
      //ypp 分页
      $page_all = ceil(count($all_loglist)/$pagenum);
      $this->assign("page_all", $page_all);
      $this->assign("page", $page);
      $this->assign("count", count($all_loglist));
      $this->assign("uid", $id);
      $this->display('User/moneylog');

    } elseif($type == "usermoney"){
      //ypp 分页
      $page = !empty(I('page'))?I('page'):1;
      $pagenum = 10; //每页显示条数
      $logtype = I("logtype");
      if($logtype){
        $where['type'] = $logtype;
        $this->assign('logtype', $logtype);
      }
      if($keyword){
        $_SESSION['keyword'] = $keyword;
        $where['msg'] = array("like","%$keyword%");
        $this->assign('keyword',  $_SESSION['keyword']);
      }else{
        $_SESSION['keyword'] = "";
      }

      $all_loglist = M("Log")->where($where)->order("id DESC")->count();
      $log_list = M("Log")->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
      $num = count($log_list);
      $this->assign('num', $num);
      foreach($log_list as $k => $v){
        $log_list[$k]['money'] = sctonum($v['money']);
        $log_list[$k]['username'] = M("User")->where(array("id"=>$v['user_id']))->getField("username");
      }
      $this->assign('log_list', $log_list);
      //ypp 分页
      $page_all = ceil($all_loglist/$pagenum);
      $this->assign("page_all", $page_all);
      $this->assign("page", $page);
      $this->assign("count",$all_loglist);

      $this->display('User/usermoney');

    } elseif($type == "childlist"){
      //ypp 分页
      $page = !empty(I('page'))?I('page'):1;
      $pagenum = 10; //每页显示条数
      $user = M("User");
      $startdate = I("startdate");
      $enddate = I("enddate");
      $startdate = strtotime($startdate);
      $enddate = strtotime($enddate);

      if($keyword){
        $_SESSION['keyword'] = $keyword;
        $where['username'] = array("like","%$keyword%");
        $this->assign('keyword',  $_SESSION['keyword']);
      }else{
        $_SESSION['keyword'] = "";
      }
      if($startdate > 0){
        $where['reg_time'] = array("gt",$startdate);
        $this->assign("startdate", date("Y/m/d",$startdate));
      }
      if($enddate > 0){
        $where['reg_time'] = array("lt",$enddate);
        $this->assign("enddate", date("Y/m/d",$enddate));
      }
      if($startdate>0 & $enddate>0){
        $where['reg_time'] = array(array('gt',$startdate),array('lt',$enddate), 'and') ;
      }

      $all_loglist = $user->where($where)->order("id DESC")->select();
      $list = $user->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
      foreach($list as $k => $v){
        $childnum = $user->where(array("parent"=> $v['id']))->count();
        $list[$k]['childnum'] = $childnum;
      }

      //ypp 分页
      $page_all = ceil(count($all_loglist)/$pagenum);
      $this->assign("page_all", $page_all);
      $this->assign("page", $page);
      $this->assign("count", count($all_loglist));
      $this->assign('num', count($all_loglist));
      $this->assign('keyword', $keyword);
      $this->assign('user', $list);
      $this->display('User/childlist');

    } elseif($type == "childtree"){
        $userid = M("User")->where(array("username"=>array("like","%$keyword%")))->limit(1)->getField("id");
        if($userid){
          $parent = M("User")->where(array("id"=>$userid))->select();
        }else{
          $parent = M("User")->where(array("parent" => 1))->select();
        }

        foreach($parent as $k => $v){
          $haschild = M("User")->where(array("parent" => $v['id']))->getField("id");
          if($haschild){
            $parent[$k]['has'] = 1;
          }else{
            $parent[$k]['has'] = 0;
          }
        }
      $this->assign('keyword', $keyword);
      $this->assign('parent', $parent);
      $this->display('User/childtree');
    }
    elseif($type == "adminset"){
      $user = M("Admin");
      $list = $user->where(array("name" =>array("like","%$keyword%")))->order("id DESC")->select();
      $this->assign('keyword', $keyword);
      $this->assign('adminlist', $list);
      $this->assign('admincount', count($list));
      $this->display('User/adminset');
    }
  }

  /**
   * 资金明细
   */
  public function usermoney()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    $type = !empty(I('logtype'))?I('logtype'):0;
    $day = !empty(I('day'))?I('day'):'';

    if(empty(I('page'))){
      $_SESSION["keyword"] = '';
      $_SESSION["logtype"] = '';
    }
    if (I("logtype") || $_SESSION['logtype'] !="") {
      if(I("logtype")){
        $_SESSION['logtype'] = I("logtype");
      }
      $this->assign("logtype", $_SESSION['logtype']);
    }
    $this->assign("day", $day);
    //昨天
    $beginYesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
    $endYesterday = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
    //今天
    $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
    $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;

    if($_SESSION["keyword"]){
      $keyword = $_SESSION["keyword"];
      $this->assign("keyword", $keyword);
      $where['msg'] = array("like","%$keyword%");
    }else{
      $where['id'] = array("gt",0);
    }

    if($type){
      if($type == 1){
        if($day == "yday"){
          if($where){
            $all_loglist = M("Log")->where(array("money"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("money"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }else{
            $all_loglist = M("Log")->where(array("money"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->count();
            $log_list = M("Log")->where(array("money"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }elseif($day == "tday"){
          if($where){
            $all_loglist = M("Log")->where(array("money"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("money"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }else{
            $all_loglist = M("Log")->where(array("money"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->count();
            $log_list = M("Log")->where(array("money"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }else{
          if($where){
            $all_loglist = M("Log")->where(array("money"=>array("neq",0)))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("money"=>array("neq",0)))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }else{
            $all_loglist = M("Log")->where(array("money"=>array("neq",0)))->order("id DESC")->count();
            $log_list = M("Log")->where(array("money"=>array("neq",0)))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }
      } elseif($type == 2){
        if($day == "yday"){
          if($where){
            $all_loglist = M("Log")->where(array("huoyue"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();

          }else{
            $all_loglist = M("Log")->where(array("huoyue"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }elseif($day == "tday"){
          if($where){
            $all_loglist = M("Log")->where(array("huoyue"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }else{
            $all_loglist = M("Log")->where(array("huoyue"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }else{
          if($where){
            $all_loglist = M("Log")->where(array("huoyue"=>array("neq",0)))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue"=>array("neq",0)))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }else{
            $all_loglist = M("Log")->where(array("huoyue"=>array("neq",0)))->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue"=>array("neq",0)))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }
      } elseif($type == 3){
        if($day == "yday"){
          if($where){
            $all_loglist = M("Log")->where(array("huoyue_jc"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue_jc"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();

          }else{
            $all_loglist = M("Log")->where(array("huoyue_jc"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue_jc"=>array("neq",0),"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }elseif($day == "tday"){
          if($where){
            $all_loglist = M("Log")->where(array("huoyue_jc"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue_jc"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }else{
            $all_loglist = M("Log")->where(array("huoyue_jc"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue_jc"=>array("neq",0),"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }else{
          if($where){
            $all_loglist = M("Log")->where(array("huoyue_jc"=>array("neq",0)))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue_jc"=>array("neq",0)))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }else{
            $all_loglist = M("Log")->where(array("huoyue_jc"=>array("neq",0)))->order("id DESC")->count();
            $log_list = M("Log")->where(array("huoyue_jc"=>array("neq",0)))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }
      }else{
        if($day == "yday"){
          if($where){
            $all_loglist = M("Log")->where(array("type"=>$type,"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("type"=>$type,"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();

          }else{
            $all_loglist = M("Log")->where(array("type"=>$type,"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->count();
            $log_list = M("Log")->where(array("type"=>$type,"date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }elseif($day == "tday"){
          if($where){
            $all_loglist = M("Log")->where(array("type"=>$type,"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("type"=>$type,"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }else{
            $all_loglist = M("Log")->where(array("type"=>$type,"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->count();
            $log_list = M("Log")->where(array("type"=>$type,"date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }else{
          if($where){
            $all_loglist = M("Log")->where(array("type"=>$type))->where($where)->order("id DESC")->count();
            $log_list = M("Log")->where(array("type"=>$type))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }else{
            $all_loglist = M("Log")->where(array("type"=>$type))->order("id DESC")->count();
            $log_list = M("Log")->where(array("type"=>$type))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
          }
        }
      }
    }else{
      if($day == "yday"){
        if($where){
          $all_loglist = M("Log")->where(array("date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->count();
          $log_list = M("Log")->where(array("date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
        }else{
          $all_loglist = M("Log")->where(array("date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->count();
          $log_list = M("Log")->where(array("date"=>array(array('gt', $beginYesterday), array('lt', $endYesterday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
        }
      }elseif($day == "tday"){
        if($where){
          $all_loglist = M("Log")->where(array("date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->count();
          $log_list = M("Log")->where(array("date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
        }else{
          $all_loglist = M("Log")->where(array("date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->count();
          $log_list = M("Log")->where(array("date"=>array(array('gt', $beginToday), array('lt', $endToday), 'and')))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
        }
      }else{
        if($where){
          $all_loglist = M("Log")->where($where)->order("id DESC")->count();
          $log_list = M("Log")->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
        }else{
          $all_loglist = M("Log")->order("id DESC")->count();
          $log_list = M("Log")->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
        }
      }
    }
      foreach($log_list as $k => $v){
        $log_list[$k]['money'] =  sctonum($v['money']);
        $log_list[$k]['huoyue'] =  ($v['huoyue']>0)?$v['huoyue']:$v['huoyue_jc'];
        $log_list[$k]['username'] = M("User")->where(array("id"=>$v['user_id']))->getField("username");
      }
    $this->assign('log_list', $log_list);

    //ypp 分页
    $page_all = ceil($all_loglist/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", $all_loglist);
    $this->assign("num", $all_loglist);

    $this->display('User/usermoney');
  }









  /**
   * 会员资金明细
   */
  public function moneylog()
  {

    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数

    if(empty(I('page'))){
      $_SESSION["keyword"] = '';
    }

    $id = I("id");
    $this->assign('uid', $id);

    if($_SESSION['keyword']){
      $this->assign("keyword", $_SESSION['keyword']);
      $keyword = $_SESSION['keyword'];
      $where['username']=array('like',"%$keyword%");
    }else{
      $_SESSION['keyword'] = "";
    }

    if($id){
      $username = M("User")->where(array("id"=>$id))->getField("username");
      $all_loglist = M("Log")->where(array("user_id"=>$id,"money"=>array("neq",0),"msg"=>array("like","%$keyword%")))->order("id DESC")->select();
      $log_list = M("Log")->where(array("user_id"=>$id,"money"=>array("neq",0),"msg"=>array("like","%$keyword%")))->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
      $this->assign('username', $username);
      $this->assign('userid', $id);
      foreach($log_list as $k => $v){
        $log_list[$k]['money'] = sctonum( $log_list[$k]['money']);
      }
      $this->assign('log_list', $log_list);
    }else{
      $this->error('请选择用户', U('User/clients'));
    }

    //ypp 分页
    $page_all = ceil(count($all_loglist)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", count($all_loglist));
    $this->assign("num", count($all_loglist));

    $this->display('User/moneylog');
  }


  /*
   * 留言反馈
   */
  public function feedback(){
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    $all_msg = M("Msg")->order("id desc")->group('msg_id')->select();
    $msg = M("Msg")->order("id desc")->group('msg_id')->limit($page*$pagenum-$pagenum,$pagenum)->select();
    foreach($msg as $k=>$v){
      $msg[$k]['username'] = M("User")->where(array("id"=>$v['user_id']))->getField("username");
      $msg[$k]['adminname'] = M("Admin")->where(array("id"=>$v['admin']))->getField("name");
    }

    //ypp 分页
    $page_all = ceil(count($all_msg)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", count($all_msg));

    $this->assign('msg', $msg);
    $this->display('User/feedback');
  }

  /*
   * 留言回复
   */
  public function answer(){
    $id = I("id");
    $msg_id = M("Msg")->where(array("id"=>$id))->getField("msg_id");
    $user_id = M("Msg")->where(array("id"=>$id))->getField("user_id");
    $msg = M("Msg")->where(array("msg_id"=>$msg_id))->order("id asc")->select();

    $title =  M("Msg")->where(array("id"=>$id))->getField("title");
    $status =  M("Msg")->where(array("id"=>$id))->getField("status");
    $username = M("User")->where(array("id"=>$user_id))->getField("username");
    $this->assign("msg_id",$msg_id);
    $this->assign("status",$status);
    $this->assign("title",$title);
    $this->assign("username",$username);
    $this->assign('msg', $msg);
    $this->display('User/answer');
  }

  /*
 * 留言回复
 */
  public function answered(){
    $msgid = I("post.msgid");
    $id = M("Msg")->where(array("msg_id"=>$msgid))->order("id desc")->getField("id");
    $answer = M("Msg")->where(array("msg_id"=>$msgid))->order("id desc")->getField("answer");
    $data['admin'] = $_SESSION['adminId'];
    if($answer){
      $data['answer'] = $answer."。".I("post.answer");
    }else{
      $data['answer'] = I("post.answer");
    }
    $data['answer_date'] = time();
    $res = M("Msg")->where(array("msg_id"=>$msgid,"id"=>$id))->save($data);
    $data1['admin'] = 1;
    $res1 = M("Msg")->where(array("msg_id"=>$msgid))->where(array("admin"=>array("eq",0)))->save($data1);
    if($res || $res1){
      $this->success('回复成功', U('User/feedback'));
    }else{
      $this->success('回复失败', U('User/feedback'));
    }
  }

  /*
* 关闭留言
*/
  public function closefk(){
    $msgid = I('msgid');
    $id = M("Msg")->where(array("msg_id"=>$msgid))->order("id asc")->getField("id");
    $date = M("Msg")->where(array("msg_id"=>$msgid))->order("id asc")->getField("answer_date");
    $answer['answer_date'] = $date;
    $data['status'] = 1;
    $res = M("Msg")->where(array("msg_id"=>$msgid))->save($data);
    if($res){
      $this->success('状态已更改', U('User/feedback'));
    }
  }

  /**
   * 团队结构
   */
  public function team()
  {
    $id = I("id");
    $this->assign("thisid", $id);
    $now_list_num = M("Sys")->where(array("name"=> 'now_list_num'))->getField('value'); //当前轮次
    if ($id) {
      if($_SESSION['keyword'] != ''){

        $user_id = M("Seatlist")->where(array("id" => $id,'list_num'=>$now_list_num))->getField("user_id");
        $username = M("User")->where(array("id" => $user_id))->getField("username");
        $keyword = $_SESSION['keyword'];
        $this->assign("keyword", $username);
      }else{
        $user_id = M("Seatlist")->where(array("id" => $id,'list_num'=>$now_list_num))->getField("user_id");
        $username = M("User")->where(array("id" => $user_id))->getField("username");
      }

    } else {
      $keyword = I("post.username");
      $_SESSION['keyword'] = $keyword;
      if ($keyword) {
        $this->assign("keyword", $keyword);

        if(!M("User")->where(array("username" => $keyword))->getField("id")){
          $user_id = M("User")->where(array("username" => array("like", "%$keyword%")))->getField("id");
        }else{
          $user_id = M("User")->where(array("username" => $keyword))->getField("id");
        }
        if (!$user_id) {
          $this->error('未查询到用户，请重新输入', U('User/team'));
        } else {
          $isseat = M("Seatlist")->where(array("user_id" => $user_id,'list_num'=>$now_list_num))->getField("id");
          if (!$isseat) {
            $this->error('该用户尚无捐赠记录！', U('User/team'));
          }
        }
        $username = M("User")->where(array("id" => $user_id))->getField("username");
      } else {
        $user_id = M("Seatlist")->where(array("line" => 1,'list_num'=>$now_list_num))->getField("user_id");
        if (!$user_id) {
          $this->error('本轮还没有捐赠过！', U('User/index'));
        }
        $username = M("User")->where(array("id" => $user_id))->getField("username");
      }
    }

    $allmyseat = M("Seatlist")->where(array("user_id" => $user_id,'list_num'=>$now_list_num))->order('id asc')->limit(10)->select();
    $_SESSION['myseat'] = $allmyseat;
    $this->assign("allmyseat",  $_SESSION['myseat']);
    $this->assign("myseat_count", count($_SESSION['myseat']));
    //我的座位信息
    if ($id) {
      $myseat = M("Seatlist")->where(array("id" => $id,'list_num'=>$now_list_num))->order('id asc')->limit(1)->select();
    } else {
      $myseat = M("Seatlist")->where(array("user_id" => $user_id,'list_num'=>$now_list_num))->order('id asc')->limit(1)->select();
    }

    //我的下线1座位信息
    $mytree1 = M("Seatlist")->where(array("line" => $myseat[0]['line'] + 1, "seat" => $myseat[0]['seat'] * 2 - 1,'list_num'=>$now_list_num))->select();
    foreach ($mytree1 as $k => $v) {
      $mytree1[$k]['username'] = M("User")->where(array("id" => $v['user_id']))->getField("username");
    }
    //我的下线2座位信息
    $mytree2 = M("Seatlist")->where(array("line" => $myseat[0]['line'] + 1, "seat" => $myseat[0]['seat'] * 2,'list_num'=>$now_list_num))->select();
    foreach ($mytree2 as $k => $v) {
      $mytree2[$k]['username'] = M("User")->where(array("id" => $v['user_id']))->getField("username");
    }
    $num1 = $this->sumteam($myseat[0]['line'], $myseat[0]['seat'],$now_list_num);
    $num2 = $this->sumteam($myseat[0]['line'] + 1, $myseat[0]['seat'] * 2 - 1,$now_list_num);
    $num3 = $this->sumteam($myseat[0]['line'] + 1, $myseat[0]['seat'] * 2,$now_list_num);

    $this->assign("num_1_left", $num1['left']);
    $this->assign("num_1_right", $num1['right']);

    $this->assign("num_2_left", $num2['left']);
    $this->assign("num_2_right", $num2['right']);

    $this->assign("num_3_left", $num3['left']);
    $this->assign("num_3_right", $num3['right']);

    $this->assign("myseat", $myseat);
    $this->assign("mytree1", $mytree1);
    $this->assign("mytree2", $mytree2);
    $this->assign("username", $username);
    $this->display("User/team");
  }
  public function yeji(){
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数

    $Yejilist = M("Yeji")->order("id desc")->getField("yeji_id",true);
    $all_list = array_unique($Yejilist);
    $list = array_slice($all_list, $page*$pagenum-$pagenum,$pagenum); /*取排名前X*/


    $now_yj_time = M("Sys")->where(array("name"=> 'now_yj_time'))->getField('value');
    $yejilist = array();
    foreach($list as $k=>$v){
      $yejilist[$k]['yeji_id'] = $v;
      $yejilist[$k]['yeji_date'] = date("Y-m-d H:i:s",$v);
      $yejilist[$k]['yj_id'] = $k+1;
    }
    $this->assign("now_yj_time", $now_yj_time);
    $this->assign("list", $yejilist);

    $page_all = ceil(count($all_list)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", count($all_list));

    $this->display("User/yeji");
  }
  public function yejilist(){
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数

    $id = I("id");
    //业绩比拼，限定前X名。
    $rank = M("Sys")->where(array("name" => 'ranks'))->getField('value');
    //业绩比拼，限定最少推荐人数
    $rank_num = M("Sys")->where(array("name" => 'rank_num'))->getField('value');

    //$where['user_id'] = array("gt",1);
    //$allyejilist = M("Yeji")->where(array("yeji_id"=>$id))->select();
    $yejilist = M("Yeji")->where(array("yeji_id"=>$id))/*->limit($page*$pagenum-$pagenum,$pagenum)*/->select();

    $yeji = array();
    foreach($yejilist as $k =>$v){

      if($v['num']>= $rank_num){
        $yeji[$k]['username'] = M("User")->where(array("id"=>$v['user_id']))->getField("username");
        $yeji[$k]['num'] = $v['num'];
        $yeji[$k]['time'] = M("User")->where(array("id"=>$v['user_id']))->getField("activate_time");
      }
    }

    array_multisort(array_column($yeji, 'num'), SORT_DESC, array_column($yeji, 'time'), SORT_ASC, $yeji); /*按推荐数、占位时间排序*/
    $allyejilist = array_slice($yeji, 0,$rank); /*取排名前X*/
    $yeji = array_slice($yeji, $page*$pagenum-$pagenum,$pagenum); /*取排名前X*/
    //ypp 分页
    $page_all = ceil(count($allyejilist)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("page1", $page-1);
    $this->assign("pagenum", $pagenum);
    $this->assign("count", count($allyejilist));
    $this->assign("list", $yeji);
    $this->display("User/yejilist");
  }


  public function juanzeng(){
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数

    $jzlist = M("Seatlist")->order("id desc")->getField("list_num",true);
    $all_list = array_unique($jzlist);
    $list = array_slice($all_list, $page*$pagenum-$pagenum,$pagenum); /*取排名前X*/


    $now_list_num = M("Sys")->where(array("name"=> 'now_list_num'))->getField('value');
    $seatlist = array();
    foreach($list as $k=>$v){
      $seatlist[$k]['list_num'] = $v;
    }
    $this->assign("now_list_num", $now_list_num);
    $this->assign("list", $seatlist);

    $page_all = ceil(count($all_list)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", count($all_list));

    $this->display("User/juanzeng");
  }
  public function juanzenglist(){
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数
    $id =I("id");

    $lastline =  M("Seatlist")->where(array("list_num"=>$id))->order("id desc")->getField("line");
    $lastseat =  M("Seatlist")->where(array("list_num"=>$id))->order("id desc")->getField("seat");
    $duo  = pow(2,$lastline-1)-$lastseat;


    $all_list = 1;
    while($lastline >1){
      $all_list = $all_list + pow(2,$lastline-1);
      $lastline = $lastline-1;
    }
    $all_list = $all_list-$duo;
    //$all_list = M("Seatlist")->where(array("list_num"=>$id))->order("id desc")->select();
    $list = M("Seatlist")->where(array("list_num"=>$id))->order("id desc")->limit($page*$pagenum-$pagenum,$pagenum)->select();

    foreach($list as $k=>$v){
      $list[$k]['username'] = M("User")->where(array("id"=>$v['user_id']))->getField("username");
    }
    $this->assign("list", $list);

    $page_all = ceil($all_list/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", $all_list);
    $this->display("User/juanzenglist");
  }

  function sumteam($line, $seat,$now_list_num)
  {
    $newline = $line;  //初始行数
    $newseat = $seat;   //初始位置

    $num_l = 0;
    $num_r = 0;

/*    $endline = M("Seatlist")->where(array("list_num"=>$now_list_num))->order("id DESC")->limit(1)->getField("line");
    $h_seat = pow(2,$endline-1)/2;

    if($newseat > $h_seat){ //最后在左边

    }else{  //最后在右边
      //统计左边总个数
      $w = 1;
      while ($w >0) {
        $num_l += $h_seat;
        $endline = $endline-1;
        $h_seat = pow(2,$endline-1)/2;
        $w = $endline-$newline;
      }
    }
    echo $num_l;die();*/


     $newseat_l = array($newseat * 2 - 1);
     $newseat_r = array($newseat * 2);
     $seat_l = $newseat_l;
     $seat_r = $newseat_r;
     $line_l = $newline;
     $line_r = $newline;
     //统计左边总个数
    $is_l = true;
     while ($is_l == true) {
       $seat1 = array();
       foreach ($seat_l as $k => $v) {
         $treeid = M("Seatlist")->where(array("line" => $line_l + 1, "seat" => $v,"list_num"=>$now_list_num))->getField("id");
         if ($treeid) {
           $num_l += 1;
           $seat1[] = $v * 2 - 1;
           $seat1[] = $v * 2;
         } else {
           $is_l = false;
         }
       }
       $seat_l = $seat1;
       $line_l = $line_l + 1;
     }
     //统计右边总个数
     $is_r = true;
     while ($is_r == true) {
       $seat2 = array();
       foreach ($seat_r as $k => $v) {
         $treeid = M("Seatlist")->where(array("line" => $line_r + 1, "seat" => $v,"list_num"=>$now_list_num))->getField("id");
         if ($treeid) {
           $num_r += 1;
           $seat2[] = $v * 2 - 1;
           $seat2[] = $v * 2;
         } else {
           $is_r = false;
         }
       }
       $seat_r = $seat2;
       $line_r = $line_r + 1;
     }


    $num = array("left" => $num_l, "right" => $num_r);
    return $num;
  }
  /**
   * ypp 会员结构
   */
  public function childtree()
  {
    $parent_id = I("get.parent");
    $userid = I("get.userid");

    if($parent_id){
      $parent = M("User")->where(array("parent" => $parent_id))->select();
      foreach($parent as $k => $v){
        $haschild = M("User")->where(array("parent" => $v['id']))->getField("id");
        if($haschild){
          $parent[$k]['has'] = 1;
        }else{
          $parent[$k]['has'] = 0;
        }
      }
      $this->ajaxReturn($parent,'JSON');
    }else{
      if($userid){
        $parent = M("User")->where(array("id"=>$userid))->select();
      }else{
        $parent = M("User")->where(array("parent" =>1))->select();
      }

      foreach($parent as $k => $v){
        $haschild = M("User")->where(array("parent" => $v['id']))->getField("id");
        if($haschild){
          $parent[$k]['has'] = 1;
        }else{
          $parent[$k]['has'] = 0;
        }
      }
      $this->assign('parent', $parent);
    }
    $this->display('User/childtree');
  }


  /**
   * ypp 推荐列表
   */
  public function childlist()
  {
    //ypp 分页
    $page = !empty(I('page'))?I('page'):1;
    $pagenum = 10; //每页显示条数

    if(empty(I('page'))){
      $_SESSION["keyword"] = '';
    }

    if($_SESSION["keyword"]){
      $keyword = $_SESSION["keyword"];
      $this->assign("keyword", $keyword);
      $where['username'] = array("like","%$keyword%");
    }else{
      $where['id'] = array("gt",0);
    }

    $user = M("User");
    $id = I('id');
    if($id){
      $all_loglist = $user->where(array("parent"=>$id,"id"=>array("gt",0)))->where($where)->order("id DESC")->select();
      $list = $user->where(array("parent"=>$id,"id"=>array("gt",0)))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }else{
      $all_loglist = $user->where(array("id"=>array("gt",0)))->where($where)->order("id DESC")->select();
      $list = $user->where(array("id"=>array("gt",0)))->where($where)->order("id DESC")->limit($page*$pagenum-$pagenum,$pagenum)->select();
    }
    foreach($list as $k => $v){
      $childnum = $user->where(array("parent"=> $v['id']))->count();
      $list[$k]['childnum'] = $childnum;
    }
    //ypp 分页
    $page_all = ceil(count($all_loglist)/$pagenum);
    $this->assign("page_all", $page_all);
    $this->assign("page", $page);
    $this->assign("count", count($all_loglist));
    $this->assign("num", count($all_loglist));
    $this->assign('user', $list);
    $this->display('User/childlist');
  }

  /**
   * ypp 系统设置
   */
  public function sysset(){
    $sys = M('Sys')->select();
    foreach($sys as $k=>$v){
      $this->assign(''.$v['name'], $v['value']);
    }
    $this->display('User/sysset');
  }
  /**
   * ypp 系统设置处理
   */
  public function syssetdone(){
    $data['sys_name'] = I("post.sys_name");
    $sys_on = I("post.sys_on");
    $data['sys_on'] = $sys_on;
    if($sys_on == 1){
      $sys_off =  I("post.sys_off");
      if($sys_off){
        $data['sys_off'] = $sys_off;
      }else{
        $data['sys_off'] = "系统已关闭！";
      }
    }
    $data['reg_on'] = I("post.reg_on");
    $data['currency_name'] = I("post.currency_name");
    $data['wallet_name'] = I("post.wallet_name");
    $data['seat_name'] = I("post.seat_name");
    $data['seat_num'] = I("post.seat_num");
    $data['recommend_name'] = I("post.recommend_name");
    $data['seat_on'] = I("post.seat_on");
    $data['jz_countdown'] = I("post.jz_countdown");
    $data['yj_countdown'] = I("post.yj_countdown");
    $data['seat_money'] = I("post.seat_money");
    $data['in_sys'] = I("post.in_sys");
    $data['in_pond'] = I("post.in_pond");
    $data['in_list'] = I("post.in_list");
    $data['rank_num'] = I("post.rank_num");
    $data['ranks'] = I("post.ranks");
    $data['rank_deuce'] = I("post.rank_deuce");
    $data['wallet'] = I("post.wallet");
    $data['recommend_on'] = I("post.recommend_on");
    $data['recommend_num'] = I("post.recommend_num");
    $data['cz_on'] = I("post.cz_on");
    $data['tx_on'] = I("post.tx_on");
    $data['min_tx'] = I("post.min_tx");
    $data['wlsxf'] = I("post.wlsxf");
    $data['jc_text'] = I("post.jc_text");

    $data['huoyue_jl'] = I("post.huoyue_jl");
    $data['huoyue_jc'] = I("post.huoyue_jc");
    $data['min_price'] = I("post.min_price");
    $data['max_num'] = I("post.max_num");
    $data['day_num'] = I("post.day_num");
    $data['usdt_lv'] = I("post.usdt_lv");

    foreach($data as $k=>$v){
      $a['value'] = $v;
      M("Sys")->where(array("name"=>$k))->save($a);
    }

    $pond = M("Sys")->where(array("name"=>"pond"))->getField("value");
    if(I("post.seat_on") == 1 && $pond == 0){
      //当打开占位开关，并且当前没有人占位，执行系统占位。
      $this->sysseat(1,1);
      $this->sysseat(2,1);
      $this->sysseat(2,2);
      $this->sysseat(3,1);
      $this->sysseat(3,2);
      $this->sysseat(3,3);
      $this->sysseat(3,4);
      $this->sysseat(4,1);
      $this->sysseat(4,2);
      $this->sysseat(4,3);
      $this->sysseat(4,4);
      $this->sysseat(4,5);
      $this->sysseat(4,6);
      $this->sysseat(4,7);
      $this->sysseat(4,8);
      $data['value'] = time();
      M("Sys")->where(array("name"=>"now_list_time"))->save($data);

      $in_list = M("Sys")->where(array("name"=>"in_list"))->getField("value");
      $in_pond = M("Sys")->where(array("name"=>"in_pond"))->getField("value");
      $l['value'] = $in_list*15;
      M("Sys")->where(array("name"=>"list"))->save($l);
      $p['value'] = $in_pond*15;
      M("Sys")->where(array("name"=>"pond"))->save($p);

    }

    $this->success('修改成功', U('User/sysset'));
  }

//系统占位
  function sysseat($line,$seat){
    $time =time();
    $seatmoney = M("Sys")->where(array("name"=>"seat_money"))->getField("value");
    $now_list_num = M("Sys")->where(array("name"=>"now_list_num"))->getField("value");
    $yj_list_num = M("Sys")->where(array("name"=>"now_yj_time"))->getField("value");

    $data['user_id'] = 1;
    $data['seat_money'] = $seatmoney;
    $data['tree_id'] = 1;
    $data['parent_id'] = 1;
    $data['line'] = $line;
    $data['seat'] = $seat;
    $data['time'] = $time;
    $data['list_num'] = $now_list_num;
    $data['yj_list_num'] = $yj_list_num;
    M("Seatlist")->add($data);
  }

  /**
   * ypp 会员等级设置
   */
  public function userset(){
    $userset = M('Userclass')->select();
    foreach($userset as $k=>$v){
      $kj = M('Kuangji')->select();
      foreach($kj as $key=>$val){
        if($v['kjjl'] == $val['id']){
          $kj[$key]['selected'] = 1;
        }else{
          $kj[$key]['selected'] = 0;
        }
      }
      $userset[$k]['kj'] = $kj;
    }

    $this->assign("userset", $userset);
    $this->assign("B", $this->B);
    $this->display('User/userset');
  }

  /**
   * ypp 会员等级设置处理
   */
  public function usersetdone(){
    for($i=1;$i<6;$i++){
      $data['class_name'][] = $_POST['class_name_'.$i];
      $data['need_num'][] = $_POST['need_num_'.$i];
      $data['team_huoyue'][] = $_POST['team_huoyue_'.$i];
      $data['youxiao_huoyue'][] = $_POST['youxiao_huoyue_'.$i];
      $data['kjjl'][] = $_POST['kjjl_'.$i];
      $data['bi_sxf'][] = $_POST['bi_sxf_'.$i];
      $data['class_reward'][] = $_POST['class_reward_'.$i];
    }
    $i = 1;
    while($i<6){
      $a['class_name'] =  $data['class_name'][$i-1];
      $a['need_num'] =  $data['need_num'][$i-1];
      $a['team_huoyue'] =  $data['team_huoyue'][$i-1];
      $a['youxiao_huoyue'] =  $data['youxiao_huoyue'][$i-1];
      $a['kjjl'] =  $data['kjjl'][$i-1];
      $a['bi_sxf'] =  $data['bi_sxf'][$i-1];
      $a['class_reward'] =  $data['class_reward'][$i-1];
      M("Userclass")->where("class = ".$i)->save($a);
      $i++;
    }
    $this->success('设置成功！', U("User/userset"));

  }

  /**
   * ypp 推荐层级设置
   */
  public function recommendset(){
    $sys = M('Sys')->select();
    foreach($sys as $k=>$v){
      $this->assign(''.$v['name'], $v['value']);
    }
    $this->display('User/recommendset');
  }
  public function recommendsetdone(){
    $data['recommend_1'] = I("post.recommend_1");
    $data['recommend_2'] = I("post.recommend_2");
    $data['recommend_3'] = I("post.recommend_3");
    $data['recommend_4'] = I("post.recommend_4");
    $data['recommend_5'] = I("post.recommend_5");
    $data['recommend_6'] = I("post.recommend_6");
    $data['recommend_7'] = I("post.recommend_7");
    $data['recommend_8'] = I("post.recommend_8");
    $data['recommend_9'] = I("post.recommend_9");
    $data['recommend_10'] = I("post.recommend_10");
    foreach($data as $k=>$v){
      $a['value'] = $v;
      M("Sys")->where(array("name"=>$k))->save($a);
    }
    $this->success('修改成功', U('User/recommendset'));
  }


  public function adminset(){
    $adminlist = M("Admin")->select();
    $this->assign("admincount", count($adminlist));
    $this->assign("adminlist", $adminlist);
    $this->display('User/adminset');
  }

  /**
   * 编辑管理员
   */
  public function adminedit(){
    $id = I("id");


    $admininfo = M("Admin")->where(array("id"=>$id))->select();
    foreach($admininfo as $k => $v){

      for($i=0;$i<10;$i++){
        $pos = strpos("$v[adminpower]", "$i");
        if($pos !== false){
          $this->assign("adminpower".$i, 1);
        }
      }
    }

    $this->assign("admininfo", $admininfo);
    $this->display('User/adminedit');
  }

  public function admineditdone(){
    $adminname = I("post.adminname");
    $adminpass = I("post.adminpass");
    $adminid = I("post.adminid");
    $issupper = I("post.issupper");
    $adminpower = I("post.adminpower");
    $power = "";
    if($issupper == 1){
      $power = '0';
    }else{
      foreach($adminpower as $k => $v){
        $power .= $v.",";
      }
    }

    if($adminname != ''){
      $is_admin = M("Admin")->where("name = '$adminname'")->getField("id");
      if($is_admin){
        $this->error('管理员账号已存在', U("User/adminadd"));
      }
      if(strlen($adminname) < 6){
        $this->error('管理员账号不能少于6位', U("User/adminadd"));
      }
      $data['name'] = $adminname;
    }

    if($adminpass != ''){
     if(strlen($adminpass) < 6){
        $this->error('管理员密码不能少于6位', U("User/adminadd"));
      }
      $data['password'] = md5($adminpass);
    }


    if($power === ""){
      $this->error('管理权限不能为空', U("User/adminset"));
    }else{
      $data['adminpower'] = $power;
    }
    $s = M("Admin")->where(array("id"=>$adminid))->save($data);
    if($s){
      $this->success('保存成功', U("User/adminset"));
    }else{
      $this->error('未作更改', U("User/adminset"));
    }
  }

  //添加管理员
  public function adminadd(){
    $this->display('');
  }
  //添加管理员
  public function adminadddone(){
    $adminname = I("post.adminname");
    $adminpass = I("post.adminpass");
    $issupper = I("post.issupper");
    $adminpower = I("post.adminpower");
    $power = "";
    if($issupper == 1){
      $power = '0';
    }else{
      foreach($adminpower as $k => $v){
        $power .= $v.",";
      }
    }
    $is_admin = M("Admin")->where("name = '$adminname'")->getField("id");
    if($is_admin){
      $this->error('管理员账号已存在', U("User/adminadd"));
    }

    if($adminname == ""){
      $this->error('管理员账号不能为空', U("User/adminadd"));
    }elseif(strlen($adminname) < 6){
      $this->error('管理员账号不能少于6位', U("User/adminadd"));
    }

    if($adminpass == ""){
      $this->error('管理员密码不能为空', U("User/adminadd"));
    }elseif(strlen($adminpass) < 6){
      $this->error('管理员密码不能少于6位', U("User/adminadd"));
    }

    if($power === ""){
      $this->error('管理权限不能为空', U("User/adminadd"));
    }else{
      $data['adminpower'] = $power;
    }

    $data['name'] = $adminname;
    $data['password'] = md5($adminpass);
    $s = M("Admin")->add($data);
    if($s){
      $this->success('添加成功', U("User/adminset"));
    }else{
      $this->error('添加失败，请重试', U("User/adminset"));
    }
  }
  /**
   * 编辑矿机
   */
  public function kj_edit()
  {
    $kuangji = M("Kuangji");
    $id = I("id");
    $thiskuangji = $kuangji->where("id=$id")->select();
    $this->assign('thiskuangji',  $thiskuangji);
    $this->display('User/kj_edit');
  }
  /**
   * 编辑矿机处理
   */
  public function kj_done()
  {
    $kuangji = M("Kuangji");
    $id = I("post.kjid");
    $data['name'] = I("post.name");
    $is_name = $kuangji->where(array('name'=>I("post.name")))->getField('id');
    if($is_name!=$id && $is_name>0){
      $this->error('矿机名不能重复', U('User/kj_edit',array("id"=>$id)));
    }

    //$data['type'] = $_POST['type'];
    $data['price'] = I("post.price");
   /* $data['price2_s'] = I("post.price2_s");
    $data['price2_e'] = I("post.price2_e");
    $count = 0;
    $temp = explode ( '.', I("post.price2_s"));
    if (sizeof ( $temp ) > 1) {
      $decimal = end ( $temp );
      $count = strlen ( $decimal );
    }
    $count1 = 0;
    $temp1 = explode ( '.', I("post.price2_e"));
    if (sizeof ( $temp1 ) > 1) {
      $decimal1 = end ( $temp1 );
      $count1 = strlen ( $decimal1 );
    }

    if($count >1 || $count1 > 1){
      $this->error("价格范围最多输入1位小数", U('User/kj_edit',array("id"=>$id)));
      die();
    }*/


    $data['cycle'] = I("post.cycle");
    $data['lilv'] = I("post.lilv");
    $data['day_num'] = I("post.day_num");
    $data['max_num'] = I("post.max_num");
   /* $data['day_money'] = I("post.day_money");
    $data['need_seat'] = I("post.need_seat");
    $data['shifang'] = I("post.shifang");
    $data['tqsf'] = I("post.tqsf");*/
    $data['msg'] = I("post.msg");
    $data['is_show'] = I("post.is_show");
    $res = $kuangji->where(array("id"=>$id))->save($data);
    if($res){
      $this->success('修改成功', U('User/kuangji'));
    } else {
      $this->success('未作更改', U('User/kj_edit',array("id"=>$id)));
    }
  }
  /**
   * 添加矿机
   */
  public function kj_add()
  {
    $this->display('User/kj_add');
  }
  /**
   * 编辑矿机处理
   */
  public function kjadd_done()
  {
    $kuangji = M("Kuangji");
    $data['name'] = I("post.name");
    $is_name = $kuangji->where(array('name'=>I("post.name")))->getField('id');
    $data['type'] = I("post.type");
    $data['price'] = I("post.price");
   /* $data['price2_s'] = I("post.price2_s");
    $data['price2_e'] = I("post.price2_e");
    $count = 0;
    $temp = explode ( '.', I("post.price2_s"));
    if (sizeof ( $temp ) > 1) {
      $decimal = end ( $temp );
      $count = strlen ( $decimal );
    }
    $count1 = 0;
    $temp1 = explode ( '.', I("post.price2_e"));
    if (sizeof ( $temp1 ) > 1) {
      $decimal1 = end ( $temp1 );
      $count1 = strlen ( $decimal1 );
    }

    if($count >1 || $count1 > 1){
      $this->error("价格范围最多输入1位小数", U('User/kj_edit',array("id"=>$id)));
      die();
    }*/

    $data['cycle'] = I("post.cycle");
    $data['lilv'] = I("post.lilv");
    $data['day_num'] = I("post.day_num");
    $data['max_num'] = I("post.max_num");
  /*  $data['day_money'] = I("post.day_money");
    $data['need_seat'] = I("post.need_seat");
    $data['shifang'] = I("post.shifang");
    $data['tqsf'] = I("post.tqsf");*/
    $data['msg'] = I("post.msg");
    $data['addtime'] = time();
    $data['is_show'] = I("post.is_show");
    if($is_name>0){
      $this->error('该产品名已存在', U('User/kj_add'));
    }
    if(!I("post.name")){
      $this->error('产品不能为空', U('User/kj_add'));
    }elseif(!I("post.cycle")){
      $this->error('周期不能为空', U('User/kj_add'));
    }/*elseif(!I("post.lilv")){
      $this->error('利率不能为空', U('User/kj_add'));
    }*/

    /*if(I("post.type") == 1){
      if(!I("post.day_num")){
        $this->error('每日购买数量不能为空', U('User/kj_add'));
      }
    }else{
      if(!I("post.day_money")){
        $this->error('每日额度不能为空', U('User/kj_add'));
      }
    }*/

    $res = $kuangji->add($data);
    if($res){
      $this->success('添加产品成功', U('User/kuangji'));
    } else {
      $this->success('添加产品失败', U('User/kj_add'));
    }
  }

  /**
   * 编辑用户
   */
  public function edit()
  {
    $user = M("User");
    $id = I("id");
    $thisuser = $user->where("id=$id")->select();
    $parent = $user->where("id=$id")->getField('parent');
    if($parent > 0){
      $parentname = $user->where("id=$parent")->getField('username');
    }
    $this->assign('parent', $parentname);
    foreach($thisuser as $k => $v){
      $thisuser[$k]['usermoney'] = sctonum( $thisuser[$k]['usermoney']);
      $thisuser[$k]['sellmoney'] = sctonum( $thisuser[$k]['sellmoney']);
      $thisuser[$k]['freeze_money'] = sctonum( $thisuser[$k]['freeze_money']);
    }
    $this->assign('thisuser',  $thisuser);
    $this->display('User/edit');
  }

  public function done()
  {
    $user = M("user");
    $id = I("post.uid");
    $users =  M("User")->where(array("id"=>$id))->find();
    $password = I("post.password");
    $status = I("post.status");
    if($status === 0){
      $data["status"] = '0';
    }else{
      $data["status"] = $status;
    }
    if($password){
      $data['password'] = md5($password);
    }
    $parent = I("post.parent");
    $parent_id = M("User")->where(array("username"=>$parent))->getField("id");
    if($parent && !$parent_id){
      $this->success('上级用户不存在', U('User/edit',array("id"=>$id)));
      die;
    }else{
      $data['parent'] = $parent_id;
    }
    $data['fuwushang'] = I("post.fuwushang");
    if($users['usermoney'] != I("post.usermoney")){
      $data['usermoney'] = I("post.usermoney");
      $str = "修改".$users['username']."余额，".$users['usermoney']."——".$data['usermoney'];
      add_log($id, $data['usermoney'],0, 10, $str);
    }
    if($users['sellmoney'] != I("post.sellmoney")){
      $data['sellmoney'] = I("post.sellmoney");
      $str = "修改".$users['username']."USTD，".$users['sellmoney']."——".$data['sellmoney'];
      add_log($id, $data['sellmoney'],0, 11, $str);
    }


    //$data['qbdz'] = I("post.qbdz");
    //$data['txdz'] = I("post.txdz");
    $res = $user->where(array("id"=>$id))->save($data);
    if($res){
      $this->success('修改成功', U('User/clients'));
    } else {
      $this->success('未作更改', U('User/edit',array("id"=>$id)));
    }
  }

  /**
   * 删除矿机
   */
  public function kj_delete()
  {
    $id = I("id");
    $kuangji = M("Kuangji");
    $res = $kuangji->where("id=$id")->delete();
    if ($res) {
      $this->success('删除成功', U('User/kuangji'));
    } else {
      $this->success('删除失败', U('User/kuangji'));
    }
  }
  /**
   * 删除用户
   */
  public function delete()
  {
    $id = I("id");
    $user = M("User");
    $res = $user->where("id=$id")->delete();
    if ($res) {
      $this->success('删除成功', U('User/index'));
    } else {
      $this->success('删除失败', U('User/index'));
    }
  }
  /**
   * 删除留言
   */
  public function deletemsg()
  {
    $id = I("id");
    $msg = M("Msg");
    $msgid = $msg->where("id=$id")->getField("msg_id");
    $res = $msg->where("msg_id=$msgid")->delete();
    if ($res) {
      $this->success('删除成功', U('User/feedback'));
    } else {
      $this->success('删除失败', U('User/feedback'));
    }
  }
  /**
   * 删除管理员
   */
  public function deleteadmin()
  {
    $id = I("id");
    $user = M("Admin");
    $res = $user->where("id=$id")->delete();
    if ($res) {
      $this->success('删除成功', U('User/adminset'));
    } else {
      $this->success('删除失败', U('User/adminset'));
    }
  }


  public function addmoney()
  {
    header("Content-Type:text/html;charset=UTF-8");
    $id = I("uid");
    $usermoney = M("User")->where(array("id"=>$id))->getField("usermoney");
    if(empty($usermoney)){
      echo "无此用户!";die();
    }else{
      $data['usermoney'] = $usermoney + 0;
    }
    $res = M("User")->where("id=$id")->save($data);
    if ($res) {
      echo "添加余额成功!";die();
    } else {
      echo "添加余额失败!";die();
    }
  }

}


